﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ipFoo.Webservicex;

namespace ipFoo.Helpers
{
    internal static class Advanced
    {
        /// <summary>
        /// Executes one of the advanced features
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static string Run(Models.AdvancedFeatureModel model)
        {
            switch (model.Action)
            {
                case "whois":
                    return WhoisLookup(model.Lookup);
                case "iptohost":
                    return IpAddress.Host(model.Lookup);
                case "hosttoip":
                    return IpAddress.Ip(model.Lookup);
            }

            return Resources.ResponseStrings.NoCommands;
        }

        internal static Models.AdvancedFeaturePostbackModel Validate(Models.AdvancedFeatureModel model)
        {
            var response = new Models.AdvancedFeaturePostbackModel { Data = model };
            string[] actions = {"whois", "iptohost", "hosttoip"};

            // Validate
            if (string.IsNullOrWhiteSpace(model.Lookup))
                response.ValidLookup = false;
            if (string.IsNullOrWhiteSpace(model.Action) || !actions.Contains(model.Action))
                response.ValidAction = false;
            if (!Captcha.ValidRequest(model))
                response.ValidCaptcha = false;

            return response;
        }

        /// <summary>
        /// Performs lookup of WHOIS data for passed domain name
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        /// <remarks>Thanks to http://www.webservicex.net/ for the web service!</remarks>
        private static string WhoisLookup(string host)
        {
            var whois = new whoisSoapClient();
            var response = whois.GetWhoIS(host);

            return response;
        }
    }
}